﻿#pragma execution_character_set("utf-8")
#include <QCoreApplication>
#include <QTextStream>
#include <QDebug>
#include "Operation.h"
#include "OperationFactory.h"

QTextStream cin(stdin, QIODevice::ReadOnly);
QTextStream cout(stdout, QIODevice::WriteOnly);
QTextStream cerr(stderr, QIODevice::WriteOnly);

bool isOperator(QString str);
bool isDouble(QString str);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    try {
        Operation* oper;

        qDebug() << "请选择运算符号(+、-、*、/): ";
        QString strOperate;
        cin >> strOperate;
        if (!isOperator(strOperate)) {
            throw QString("运算符号错误！");
        }
        oper = OperationFactory::createOperate(strOperate);

        qDebug() << "请输入数字A: ";
        QString strNumberA;
        cin >> strNumberA;
        if (!isDouble(strNumberA)) {
            throw QString("数字A不是浮点数！");
        }
        oper->setNumberA(strNumberA.toDouble());

        qDebug() << "请输入数字B: ";
        QString strNumberB;
        cin >> strNumberB;
        if (!isDouble(strNumberB)) {
            throw QString("数字B不是浮点数！");
        }
        oper->setNumberB(strNumberB.toDouble());

        QString strResult = QString::number(oper->GetResult());
        qDebug() << "result is" << strResult;

        delete oper;
    }
    catch (QString err) {
        qDebug() << "您的输入有错:" << err;
    }
    qDebug() << "程序结束！";

    return a.exec();
}

bool isDouble(QString str)
{
    bool ok;
    str.toDouble(&ok);
    return ok;
}

bool isOperator(QString str)
{
    bool ret;
    if (str != "+" && str != "-" && str != "*" && str != "/") {
        ret = false;
    }
    else {
        ret = true;
    }
    return ret;
}
